package com.jk.mindskip.controller;

import com.jk.mindskip.dto.TbStudent;
import com.jk.mindskip.pojo.PagedResponse;
import com.jk.mindskip.pojo.ResponseResult;
import com.jk.mindskip.service.TbStudentServiceFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "学生管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping(value = "/student")
public class TbStudentController {


    @Autowired
    private TbStudentServiceFeign tbStudentServiceFeign;

    @ApiOperation(value = "学生列表分页查询", httpMethod = "POST", notes = "学生列表分页查询")
    @PostMapping(value = "/findStudentList")
    public ResponseResult<PagedResponse<TbStudent>> findStudentList(TbStudent tbStudent, Integer pageNum, Integer pageSize){
        PagedResponse<TbStudent> pagedResponse = tbStudentServiceFeign.findStudentList(tbStudent,pageNum,pageSize);
        return new ResponseResult<>(200, "查询成功", pagedResponse);
    }


    @ApiOperation(value = "添加学生", httpMethod = "POST", notes = "添加学生")
    @PostMapping(value = "/addStudent")
    public ResponseResult addStudent(TbStudent tbStudent){
        try {
            tbStudentServiceFeign.addStudent(tbStudent);
            return new ResponseResult(200, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
           return new ResponseResult(444, "添加失败");
        }
    }



    @ApiOperation(value = "删除学生信息", httpMethod = "DELETE", notes = "删除学生信息")
    @DeleteMapping(value = "/deleteById")
    public ResponseResult deleteById(Integer id){
        try {
            tbStudentServiceFeign.deleteById(id);
            return new ResponseResult(200, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, "删除失败");
        }
    }


    @ApiOperation(value = "批量删除学生信息", httpMethod = "DELETE", notes = "批量删除学生信息")
    @DeleteMapping(value = "/deleteStudentByIds")
    public ResponseResult deleteStudentByIds(@RequestBody List<Integer> ids){
        try {
            tbStudentServiceFeign.deleteStudentByIds(ids);
            return new ResponseResult(200, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, "删除失败");
        }
    }


    @ApiOperation(value = "学生信息修改", httpMethod = "PUT", notes = "学生信息修改")
    @PutMapping(value = "/updateStudent")
    public ResponseResult updateStudent(@RequestBody TbStudent tbStudent){
        try {
            tbStudentServiceFeign.updateStudent(tbStudent);
            return new ResponseResult(200, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, "修改失败");
        }
    }

}
